import { createRouter, createWebHistory } from "vue-router";
import commonStore from "@/stores/index";
import { ElLoading } from "element-plus";
import { i18n } from "@/locale";
const useI18n: any = i18n.global;
const t = useI18n.t;

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/login/index.vue"),
    },
    {
      path: "/home",
      name: "home",
      component: () => import("@/views/home/index.vue"),
    },
    {
      path: "/layout",
      name: "",
      component: () => import("@/layout/index.vue"),
      children: [
        {
          path: "/data-lake",
          name: "",
          component: () => import("@/views/data-lake/index.vue"),
          meta: {
            breadcrumdNames: ["空间", "城市地理信息"],
          },
        },
        {
          path: "/user-management",
          name: "",
          component: () => import("@/views/user-management/index.vue"),
        },
      ],
    },
    {
      path: "/user-center",
      name: "user-center",
      component: () => import("@/views/user-center/index.vue"),
    },
  ],
});
function showLoading() {
  //路由跳转过度loading
  let loadingInstance: any = {};
  router.beforeEach((to, from, next) => {
    loadingInstance = ElLoading.service();
    next();
  });
  router.afterEach((transition: any) => {
    loadingInstance.close();
    let breadcrumdNames: Array<string> = [];
    if (transition && transition.meta && transition.path) {
      let path = transition.path;
      try {
        // 此次还需要再修改
        if (path.includes("data-lake")) {
          let arr = JSON.parse(t("breadcrumb." + path));
          breadcrumdNames = arr || [];
        }
      } catch (e) {}
    }
    // if (transition && transition.meta && transition.meta.breadcrumdNames) {
    //   breadcrumdNames = transition.meta.breadcrumdNames || [];
    // }
    const common = commonStore();
    common.setBreadcrumdNames(breadcrumdNames);
  });
}
showLoading();

export default router;
